package com.example.administrator.himalaya.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.entity.anchor.AnchorArray;
import com.rock.teachlibrary.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发现--主播
 */
public class AnchorAdapter extends BaseAdapter {

    private static final String TAG = AnchorAdapter.class.getName();
    private List<AnchorArray> data;
    private LayoutInflater inflater;
    ViewHolder holder;

    public AnchorAdapter(Context context, List<AnchorArray> data) {
        inflater = LayoutInflater.from(context);
        if (data != null) {
            this.data = data;
        } else {
            this.data = new ArrayList<>();
        }
    }

    public void update(List<AnchorArray> data) {
        if (data != null) {
            this.data.clear();
            this.data = data;
            notifyDataSetChanged();
        } else {
            Log.e(TAG, "update: 数据更新失败");
        }
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public AnchorArray getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.anchor_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TextView title = (TextView) holder.getView(R.id.anchor_item_title);
        TextView name0 = (TextView) holder.getView(R.id.anchor_item_name0);
        TextView name1 = (TextView) holder.getView(R.id.anchor_item_name1);
        TextView name2 = (TextView) holder.getView(R.id.anchor_item_name2);
        ImageView imageView0 = (ImageView) holder.getView(R.id.anchor_item_image0);
        ImageView imageView1 = (ImageView) holder.getView(R.id.anchor_item_image1);
        ImageView imageView2 = (ImageView) holder.getView(R.id.anchor_item_image2);


        title.setText(getItem(position).getTitle());
        name0.setText(getItem(position).getList().get(0).getNickname());
        name1.setText(getItem(position).getList().get(1).getNickname());
        name2.setText(getItem(position).getList().get(2).getNickname());
        ImageLoader.display(imageView0, getItem(position).getList().get(0).getSmallLogo());
        ImageLoader.display(imageView1, getItem(position).getList().get(1).getSmallLogo());
        ImageLoader.display(imageView2, getItem(position).getList().get(2).getSmallLogo());
        return convertView;
    }


    public static class ViewHolder {
        private View convertView = null;
        private Map<Integer, View> map;

        public ViewHolder(View convertView) {
            this.convertView = convertView;
            map = new HashMap<>();
        }

        public View getView(int resId) {
            View view = null;
            if (map.containsKey(resId)) {
                view = map.get(resId);
            } else {
                view = convertView.findViewById(resId);
                map.put(resId, view);
            }
            return view;
        }
    }
}
